﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GraphAlgorithm;

namespace GraphTestFrontEnd
{
	public partial class Form1 : Form
	{
		DataTable table = new DataTable();

		public Form1()
		{
			InitializeComponent();
		}

		private int[] GetAvgMlist( List<int[]> mList )
		{
			List<int> l1 = new List<int>();
			List<int> l2 = new List<int>();
			List<int> l3 = new List<int>();

			foreach( int[] it in mList )
			{
				l1.Add( it[0] );
				l2.Add( it[1] );
				l3.Add( it[2] );
			}

			return new int[] { (int)( Math.Round( l1.Average() ) ), (int)( Math.Round( l2.Average() ) ), (int)( Math.Round( l3.Average() ) ) };
		}

		private void test_Click( object sender, EventArgs evt )
		{
			int v = (int)vertex.Value;
			int c = (int)color.Value;
			double a = (double)( accept.Value ) / 100;
			double e = (double)( edge.Value ) / 100;
			int i = (int)iter.Value;

			DateTime start = DateTime.Now;
			int success = 0;
			List<int[]> mList = new List<int[]>();

			for( int j = 0; j < 100; j++ )
			{
				Graph G = GraphTest.Program.CreateRandomGraph( v, c, a, e, 1, 1 );
				G.EqualizeColorWeight();
				RandomColorAlgorithm.Result r = RandomColorAlgorithm.DoColoring( G, i );
				mList.Add( GraphTest.Program.MeasureGraph( G ) );
				if( r.IsSucceded ) success++;
			}

			int[] avgM = GetAvgMlist( mList );
			TimeSpan elaps = DateTime.Now - start;

			table.Rows.Add( new string[] { "" + v, "" + c, "" + a, "" + e, "" + i, "" + success, String.Format( "{0:F2} ms", elaps.TotalMilliseconds / 100 ), "" + avgM[0], "" + avgM[1], "" + avgM[2] } );


		}

		private void Form1_Load( object sender, EventArgs e )
		{
			table.Columns.Add( "Vertex" );
			table.Columns.Add( "Color" );
			table.Columns.Add( "Accept" );
			table.Columns.Add( "Edge" );
			table.Columns.Add( "Iterations" );
			table.Columns.Add( "Success" );
			table.Columns.Add( "Time" );
			table.Columns.Add( "MaxDegree" );
			table.Columns.Add( "AvgDegree" );
			table.Columns.Add( "AvgIntersect" );
			dataGridView1.DataSource = table;

		}

		private void clear_Click( object sender, EventArgs e )
		{
			while( table.Rows.Count > 0 )
			{
				table.Rows.RemoveAt( 0 );
			}
		}
	}
}
